import P20020_识别字符串中的Word和Token_增加错误处理 as P20020
import P20030_语法检测 as P20030

def Run(tokens):
    prevTokenType = ''
    prevToken = ''
    value = 0
    for token in tokens:
        if token['type'] == 'TOKEN_TYPE_OPERATOR': #不计算
            prevTokenType = token['type']
            prevToken = token['word']
        if token['type'] == 'TOKEN_TYPE_NUMBER': #进行计算
            match prevToken:
                case '+':
                    value = value + float(token['word'])
                case '-':
                    value = value - float(token['word'])

            prevTokenType = token['type']
            prevToken = token['word']        
    return(value)

if __name__ == '__main__':
    # 测试
    print("代码运行 test 1")
    expression = "-11+20.8-10+9"
    tokens = P20020.Tokenize(expression)
    P20030.SyntaxCheck(tokens)
    print("The resulst is: %s" % Run(tokens))

    print("代码运行 test 2")
    expression = "777+888-33333"
    tokens = P20020.Tokenize(expression)
    P20030.SyntaxCheck(tokens)
    print("The resulst is: %s" % Run(tokens))
